import unittest
from time import sleep
import random

from ddt import ddt, unpack, data

# from xiangmu.page.qiangshop_page import LoginPage
from xiangmu.page.qiangshop_page import LoginPage
from xiangmu.util.util import Utility


@ddt



class Add_Coupon(unittest.TestCase):


    @classmethod
    def setUpClass(cls):
        cls.page = LoginPage('Chrome')
        cls.logger.info('创建浏览器对象')

    @classmethod
    def tearDownClass(cls):
        cls.page.close()



    @unpack
    @data(*Utility.get_excel(r'report/pass_data.xlsx', 'success'))
    def add_coupon(self,name,money,condition,number,use_start_time,use_end_time,send_start_time,send_end_time):
                # try:


                # 点击商城
                self.click('x', '/html/body/div[1]/div[4]/ul/li[2]/a')
                # 点击促销
                self.click('x', '//*[@id="admincpNavTabs_shop"]/dl[3]/dt/a/h3')
                # 点击优惠券
                self.click('x', '//*[@id="admincpNavTabs_shop"]/dl[3]/dd/ul/li[5]/a')
                # 点击添加优惠券
                self.switch_to_frame('i', 'workspace')
                self.click('x', '/html/body/div[3]/div[3]/div[3]/div[1]/a/div/div/span')
                # 优惠券名称
                self.type('x', '//*[@id="name"]', name)
                # 优惠券面额
                self.type('x', '//*[@id="money"]', money)
                # 消费金额
                self.type('i', 'condition', condition)
                # 发放数量
                self.type('i', 'createnum', number)
                # 发放类型
                a = random.randint(1, 5)
                self.click('x', f'//*[@id="handleposition"]/div/dl[5]/dd/input[{a}]')
                if a == 1:
                    self.type('i', 'use_start_time',use_start_time )
                    self.type('i', 'use_end_time', use_end_time)
                else:
                    self.type('i', 'send_start_time', send_start_time)
                    self.type('i', 'send_end_time', send_end_time)
                    self.type('i', 'use_start_time', use_start_time)
                    self.type('i', 'use_end_time', use_end_time)
                # 可使用商品
                b = random.randint(1, 4)
                self.click('x', f'//*[@id="handleposition"]/div/dl[10]/dd/label[{b}]/input')
                if b == 2:
                    # 指定商品
                    self.switch_to_frame('i', 'layui-layer-iframe10')
                    self.type('x', '//*[@id="search-form2"]/div/div[4]/input[1]', '奥特曼')
                    self.click('x', '//*[@id="search-form2"]/div/div[4]/input[3]')
                    self.click('x', '//*[@id="goos_table"]/tr/td[1]/div')
                    self.click('x', '/html/body/div/div/div[5]/a')
                    self.switch_to_frame('i', 'workspace')
                elif b == 3:
                    # 三级分类
                    self.click('i', 'cat_id1')
                    self.click('x', f'//*[@id="cat_id1"]/option[{random.randint(2, 12)}]')
                    self.click('i', 'cat_id2')
                    self.click('x', f'//*[@id="cat_id1"]/option[{random.randint(2, 12)}]')
                    self.click('i', 'cat_id3')
                    self.click('x', f'//*[@id="cat_id3"]/option[2]')
                # 状态
                c = random.randint(1, 3)
                self.click('x', f'//*[@id="handleposition"]/div/dl[13]/dd/input[{c}]')
                # 确定
                self.click('x', '//*[@id="handleposition"]/div/div/a')
                sleep(5)

            # except Exception as e:
            #     print(e)
            # finally:
            #     sleep(2)



if __name__ == '__main__':
    unittest.main()

